/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app;

import data.IngredientMenu;
import java.io.IOException;
import model.SearchList;
import data.RecipeMenu;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import model.Activity;
import model.Cookbook;
import model.Ingredient;
import model.Measure;
import model.Product;
import model.Recipe;
import org.netbeans.microedition.lcdui.pda.FileBrowser;
import searchers.DietSearcher;
import searchers.NameContainsSearcher;
import searchers.Searcher;
import utils.XmlDataProvider;

/**
 * @author Marta
 */
public class AppMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private Recipe currentRecipe = new Recipe();
    SearchList listRecipes;
    SearchList resultList;
    SearchList resultListI;
    Cookbook cookbook = new Cookbook();
    String[] listNameResult;
    //String[] ingrSearch = null;
//    String nameSearch;
    int currentStep = 1;
    String[] step;
    // RecordStore - miejsce do zapisu informacji (częstość używania przepisów)
    private RecordStore rs;
    Vector czestoscNazwy;
    Vector czestoscLiczby;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command Powrót;
    private Command OWersji;
    private Command cancelCommand;
    private Command okCommand1;
    private Command exitCommand1;
    private Command Wróć;
    private Command Dalej;
    private Command Przygotowanie;
    private Command Wyszukiwanie;
    private Command Wróć1;
    private Command okCommand2;
    private Command Zmieńskładniki;
    private Command Wróć2;
    private Command backCommand1;
    private Command okCommand3;
    private Command okCommand5;
    private Command okCommand6;
    private Command okCommand4;
    private Command backCommand2;
    private Command przyklad;
    private Command screenCommand;
    private Command okCommand7;
    private Command okCommand;
    private Command exitCommand2;
    private List WyborTrybu;
    private List ListaPrzepisow;
    private Form Przepis;
    private Form PrzygotowanieLista;
    private StringItem stringItem5;
    private Form OknoWyszukiwania;
    private TextField textField1;
    private TextField textField;
    private Form DostepneSkladniki;
    private ChoiceGroup choiceGroup;
    private List ListaWynikow;
    private List OWersji1;
    private FileBrowser OtwarciePlikuZPrzepisami;
    private Ticker ticker;
    private Ticker ticker1;
    //</editor-fold>//GEN-END:|fields|0|
    private boolean favourite;
    //</editor-fold>

    /**
     * The AppMIDlet constructor.
     */
    public AppMIDlet() {
        // odczyt danych z recordstore
        try
        {
            rs = RecordStore.openRecordStore("CookbookMobile", true);
            System.out.println("Orworzylem: " + rs.getName());
            RecordEnumeration recEnum = rs.enumerateRecords(null, null, false);
            System.out.println("Ilosc rekordow: " + recEnum.numRecords());
            czestoscNazwy = new Vector();
            czestoscLiczby = new Vector();
            czestoscNazwy.removeAllElements();
            czestoscLiczby.removeAllElements();
            if(recEnum.numRecords()>0)
            {
                while (recEnum.hasNextElement())
                {
                    int rsId = recEnum.nextRecordId();
                    byte[] data = rs.getRecord(rsId);
                    ByteArrayInputStream arrayStream = new ByteArrayInputStream(data);
                    DataInputStream in = new DataInputStream(arrayStream);
                    String tmp = in.readUTF();
                    String nazwa = tmp.substring(0, tmp.indexOf(';'));
                    Integer liczba = Integer.valueOf(tmp.substring(tmp.indexOf(';')+1, tmp.length()));
                    czestoscNazwy.addElement(nazwa);
                    czestoscLiczby.addElement(liczba);
                    in.close();
                    arrayStream.close();
                }
            }
            else
            {
                ByteArrayOutputStream arrayStreamOut = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(arrayStreamOut);
                out.writeUTF("a;0");
                byte[] dataOut = arrayStreamOut.toByteArray();
                int rsId = rs.addRecord(dataOut, 0, dataOut.length);
                out.close();
                arrayStreamOut.close();
            }
            rs.closeRecordStore();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here

        Przepis = new Form("Przepis", new Item[] { });//GEN-BEGIN:|0-initialize|1|0-postInitialize
        Przepis.setTicker(getTicker());
        Przepis.addCommand(getWróć());
        Przepis.addCommand(getPrzygotowanie());
        Przepis.setCommandListener(this);//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here

    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getOtwarciePlikuZPrzepisami());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        step = listActivitiesToStep();
        if (displayable == DostepneSkladniki) {//GEN-BEGIN:|7-commandAction|1|147-preAction
            if (command == backCommand2) {//GEN-END:|7-commandAction|1|147-preAction
                // write pre-action user code here
                switchDisplayable(null, getOknoWyszukiwania());//GEN-LINE:|7-commandAction|2|147-postAction
                // write post-action user code here
            } else if (command == okCommand4) {//GEN-LINE:|7-commandAction|3|145-preAction
                // write pre-action user code here
                switchDisplayable(null, getOknoWyszukiwania());//GEN-LINE:|7-commandAction|4|145-postAction
                // write post-action user code here
                String elements = "";
                for (int i = 0; i < choiceGroup.size(); i++) {
                    if (choiceGroup.isSelected(i)) {
                        elements += choiceGroup.getString(i);
                        elements += ",";
                    }
                }
                textField1.setString(elements);
            }//GEN-BEGIN:|7-commandAction|5|56-preAction
        } else if (displayable == ListaPrzepisow) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|5|56-preAction
                // write pre-action user code here
                ListaPrzepisowAction();//GEN-LINE:|7-commandAction|6|56-postAction
                // write post-action user code here
            } else if (command == Wróć) {//GEN-LINE:|7-commandAction|7|61-preAction
                // write pre-action user code here
                switchDisplayable(null, getWyborTrybu());//GEN-LINE:|7-commandAction|8|61-postAction
                // write post-action user code here
            } else if (command == Wyszukiwanie) {//GEN-LINE:|7-commandAction|9|111-preAction
                // write pre-action user code here
                switchDisplayable(null, getOknoWyszukiwania());//GEN-LINE:|7-commandAction|10|111-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|134-preAction
        } else if (displayable == ListaWynikow) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|11|134-preAction
                // write pre-action user code here
                ListaWynikowAction();//GEN-LINE:|7-commandAction|12|134-postAction
                // write post-action user code here
            } else if (command == Wróć2) {//GEN-LINE:|7-commandAction|13|138-preAction
                // write pre-action user code here
                switchDisplayable(null, getOknoWyszukiwania());//GEN-LINE:|7-commandAction|14|138-postAction
                // write post-action user code here
                ListaWynikow = null;
            }//GEN-BEGIN:|7-commandAction|15|167-preAction
        } else if (displayable == OWersji1) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|15|167-preAction
 // write pre-action user code here
                OWersji1Action();//GEN-LINE:|7-commandAction|16|167-postAction
 // write post-action user code here
            } else if (command == Powrót) {//GEN-LINE:|7-commandAction|17|169-preAction
 // write pre-action user code here
                switchDisplayable(null, getWyborTrybu());//GEN-LINE:|7-commandAction|18|169-postAction
 // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|141-preAction
        } else if (displayable == OknoWyszukiwania) {
            if (command == backCommand1) {//GEN-END:|7-commandAction|19|141-preAction
                // write pre-action user code here
                ListaPrzepisow = null;
                switchDisplayable(null, getListaPrzepisow());//GEN-LINE:|7-commandAction|20|141-postAction
                // write post-action user code here
            } else if (command == okCommand2) {//GEN-LINE:|7-commandAction|21|127-preAction
                // write pre-action user code here
                switchDisplayable(null, getDostepneSkladniki());//GEN-LINE:|7-commandAction|22|127-postAction
                // write post-action user code here
            } else if (command == okCommand3) {//GEN-LINE:|7-commandAction|23|131-preAction
                // write pre-action user code here
                listNameResult = search();
                switchDisplayable(null, getListaWynikow());//GEN-LINE:|7-commandAction|24|131-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|25|182-preAction
        } else if (displayable == OtwarciePlikuZPrzepisami) {
            if (command == FileBrowser.SELECT_FILE_COMMAND) {//GEN-END:|7-commandAction|25|182-preAction
                // write pre-action user code here
                switchDisplayable(null, getWyborTrybu());//GEN-LINE:|7-commandAction|26|182-postAction
                // write post-action user code here
            } else if (command == exitCommand2) {//GEN-LINE:|7-commandAction|27|185-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|28|185-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|29|98-preAction
        } else if (displayable == Przepis) {
            if (command == Przygotowanie) {//GEN-END:|7-commandAction|29|98-preAction
                // write pre-action user code here
                switchDisplayable(null, getPrzygotowanieLista());//GEN-LINE:|7-commandAction|30|98-postAction
                // write post-action user code here
                stringItem5.setText(step[0]);
            } else if (command == Wróć) {//GEN-LINE:|7-commandAction|31|69-preAction
                // write pre-action user code here
                ListaPrzepisow = null;
                switchDisplayable(null, getListaPrzepisow());//GEN-LINE:|7-commandAction|32|69-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|33|108-preAction
        } else if (displayable == PrzygotowanieLista) {
            if (command == Wróć) {//GEN-END:|7-commandAction|33|108-preAction
                // write pre-action user code here
                switchDisplayable(null, Przepis);//GEN-LINE:|7-commandAction|34|108-postAction
                // write post-action user code here
                PrzygotowanieLista = null;
            } else if (command == okCommand7) {//GEN-LINE:|7-commandAction|35|163-preAction
                // write pre-action user code here
                switchDisplayable(null, getPrzygotowanieLista());//GEN-LINE:|7-commandAction|36|163-postAction
                // write post-action user code here
                stringItem5.setText(step[currentStep]);
                currentStep++;
            }//GEN-BEGIN:|7-commandAction|37|47-preAction
        } else if (displayable == WyborTrybu) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|37|47-preAction
                // write pre-action user code here
                WyborTrybuAction();//GEN-LINE:|7-commandAction|38|47-postAction
                // write post-action user code here
            } else if (command == OWersji) {//GEN-LINE:|7-commandAction|39|165-preAction
 // write pre-action user code here
                switchDisplayable(null, getOWersji1());//GEN-LINE:|7-commandAction|40|165-postAction
 // write post-action user code here
            } else if (command == exitCommand1) {//GEN-LINE:|7-commandAction|41|51-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|42|51-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|43|7-postCommandAction
        }//GEN-END:|7-commandAction|43|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|44|
    //</editor-fold>//GEN-END:|7-commandAction|44|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Powrót ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of Powrót component.
     * @return the initialized component instance
     */
    public Command getPowrót() {
        if (Powrót == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            Powrót = new Command("Powr\u00F3t", Command.BACK, 0);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return Powrót;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OWersji ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of OWersji component.
     * @return the initialized component instance
     */
    public Command getOWersji() {
        if (OWersji == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            OWersji = new Command("O wersji", Command.OK, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return OWersji;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            cancelCommand = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Wyj\u015Bcie", Command.EXIT, 0);//GEN-LINE:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|50-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: WyborTrybu ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of WyborTrybu component.
     * @return the initialized component instance
     */
    public List getWyborTrybu() {
        if (WyborTrybu == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            WyborTrybu = new List("Menu", Choice.IMPLICIT);//GEN-BEGIN:|46-getter|1|46-postInit
            WyborTrybu.append("Przepisy", null);
            WyborTrybu.append("Ulubione", null);
            WyborTrybu.addCommand(getExitCommand1());
            WyborTrybu.addCommand(getOWersji());
            WyborTrybu.setCommandListener(this);
            WyborTrybu.setSelectedFlags(new boolean[] { false, false });//GEN-END:|46-getter|1|46-postInit
            // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return WyborTrybu;
    }
    //</editor-fold>//GEN-END:|46-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: WyborTrybuAction ">//GEN-BEGIN:|46-action|0|46-preAction
    /**
     * Performs an action assigned to the selected list element in the WyborTrybu component.
     */
    public void WyborTrybuAction() {//GEN-END:|46-action|0|46-preAction
        // enter pre-action user code here
        String __selectedString = getWyborTrybu().getString(getWyborTrybu().getSelectedIndex());//GEN-BEGIN:|46-action|1|53-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Przepisy")) {//GEN-END:|46-action|1|53-preAction
                // write pre-action user code here
                ListaPrzepisow = null;
                favourite = false;
                switchDisplayable(null, getListaPrzepisow());//GEN-LINE:|46-action|2|53-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Ulubione")) {//GEN-LINE:|46-action|3|54-preAction
                // write pre-action user code here
                ListaPrzepisow = null;
                favourite = true;
                switchDisplayable(null, getListaPrzepisow());//GEN-LINE:|46-action|4|54-postAction
                // write post-action user code here
            }//GEN-BEGIN:|46-action|5|46-postAction
        }//GEN-END:|46-action|5|46-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|46-action|6|
    //</editor-fold>//GEN-END:|46-action|6|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ListaPrzepisow ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initiliazed instance of ListaPrzepisow component.
     * @return the initialized component instance
     */
    public List getListaPrzepisow() {
        if (ListaPrzepisow == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            ListaPrzepisow = new List("Lista przepis\u00F3w", Choice.IMPLICIT);//GEN-BEGIN:|55-getter|1|55-postInit
            ListaPrzepisow.addCommand(getWróć());
            ListaPrzepisow.addCommand(getWyszukiwanie());
            ListaPrzepisow.setCommandListener(this);
            ListaPrzepisow.setSelectedFlags(new boolean[] {  });//GEN-END:|55-getter|1|55-postInit
            // write post-init user code here
            String x = OtwarciePlikuZPrzepisami.getSelectedFileURL();
            XmlDataProvider xmlDataProvider =
                    new XmlDataProvider(x);
            cookbook = xmlDataProvider.importXML();
            listRecipes = cookbook.listRecipes();
            if(favourite) {
                Vector listItems = new Vector();
                Vector listItemsVal = new Vector();
                for (int i = 0; i < listRecipes.size(); i++) {
                    Recipe recipe = (Recipe) listRecipes.elementAt(i);
                    for(int j=0; j<czestoscNazwy.size(); j++) {
                        if(decodeString(recipe.getName()).equals((String)czestoscNazwy.elementAt(j))) {
                            int listItemsI = 0;
                            int v1, v2;
                            v1 = ((Integer)czestoscLiczby.elementAt(j)).intValue();
                            if(!listItems.isEmpty()) {
                                for(int k=0; k<listItems.size(); k++) {
                                    v2 = ((Integer)listItemsVal.elementAt(k)).intValue();
                                    if(v2>=v1) {
                                        listItemsI++;
                                    } else {
                                        break;
                                    }
                                }
                            }
                            listItems.insertElementAt(decodeString(recipe.getName()) + " -- otwarto "
                                    + ((Integer)czestoscLiczby.elementAt(j)).toString() + " razy", listItemsI);
                            listItemsVal.insertElementAt((Integer)czestoscLiczby.elementAt(j), listItemsI);
                        }
                    }
                }
                for(int i=0; i< listItems.size(); i++) {
                    ListaPrzepisow.append((String)listItems.elementAt(i), null);
                }
            } else {
                String list[] = new String[listRecipes.size()];
                for (int i = 0; i < listRecipes.size(); i++) {
                    Recipe recipe = (Recipe) listRecipes.elementAt(i);
                    list[i] = decodeString(recipe.getName());
                }
                for (int i = 0; i < listRecipes.size(); i++) {
                    ListaPrzepisow.append(list[i], null);
                }
            }
        }//GEN-BEGIN:|55-getter|2|
        return ListaPrzepisow;
    }
    //</editor-fold>//GEN-END:|55-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: ListaPrzepisowAction ">//GEN-BEGIN:|55-action|0|55-preAction
    /**
     * Performs an action assigned to the selected list element in the ListaPrzepisow component.
     */
    public void ListaPrzepisowAction() {//GEN-END:|55-action|0|55-preAction
        // enter pre-action user code here
        String __selectedString = getListaPrzepisow().getString(getListaPrzepisow().getSelectedIndex());//GEN-LINE:|55-action|1|55-postAction
        // enter post-action user code here
        if(__selectedString.indexOf(" -- ")>-1) {
            __selectedString = __selectedString.substring(0, __selectedString.indexOf(" -- "));
        }
        for (int i = 0; i < listRecipes.size(); i++) {
            Recipe recipe = (Recipe) listRecipes.elementAt(i);
            if (__selectedString.equalsIgnoreCase(decodeString(recipe.getName()))) {
                currentRecipe = recipe;
                switchDisplayable(null, getForm());
                break;
            }
        }
    }//GEN-BEGIN:|55-action|2|
    //</editor-fold>//GEN-END:|55-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Wróć ">//GEN-BEGIN:|60-getter|0|60-preInit
    /**
     * Returns an initiliazed instance of Wróć component.
     * @return the initialized component instance
     */
    public Command getWróć() {
        if (Wróć == null) {//GEN-END:|60-getter|0|60-preInit
            // write pre-init user code here
            Wróć = new Command("Wr\u00F3\u0107", Command.BACK, 0);//GEN-LINE:|60-getter|1|60-postInit
            // write post-init user code here
        }//GEN-BEGIN:|60-getter|2|
        return Wróć;
    }
    //</editor-fold>//GEN-END:|60-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Dalej ">//GEN-BEGIN:|87-getter|0|87-preInit
    /**
     * Returns an initiliazed instance of Dalej component.
     * @return the initialized component instance
     */
    public Command getDalej() {
        if (Dalej == null) {//GEN-END:|87-getter|0|87-preInit
            // write pre-init user code here
            Dalej = new Command("Ok", Command.OK, 0);//GEN-LINE:|87-getter|1|87-postInit
            // write post-init user code here
        }//GEN-BEGIN:|87-getter|2|
        return Dalej;
    }
    //</editor-fold>//GEN-END:|87-getter|2|

    public Form getForm() {
        Recipe recipe = currentRecipe;
        Vector listI = recipe.listIngredients();
        int sizeI = listI.size();
        String ingredient[] = new String[sizeI];
        String measure[] = new String[sizeI];
        String amount[] = new String[sizeI];
        for (int i = 0; i < sizeI; i++) {
            Ingredient first = (Ingredient) listI.elementAt(i);
            ingredient[i] = decodeString(first.getProduct().getName().toString());
            Measure miara = first.getMeasure();
            measure[i] = decodeString(miara.getName());
            amount[i] = decodeString(miara.getAmount());
        }

        int foundIndex=-1;
        for(int i=0; i<czestoscNazwy.size(); i++) {
            if(decodeString(currentRecipe.getName()).equals((String)czestoscNazwy.elementAt(i))) {
                foundIndex = i;
                break;
            }
        }
        if(foundIndex>-1) {
            Integer n = (Integer)czestoscLiczby.elementAt(foundIndex);
            System.out.print("Znalazlem " + decodeString(currentRecipe.getName()) + ", zamieniam " + n.toString());
            int n2 = n.intValue() + 1;
            n = new Integer(n2);
            System.out.print(" na " + n.toString() + "\n");
            czestoscLiczby.insertElementAt(n, foundIndex);
            czestoscLiczby.removeElementAt(foundIndex+1);
        } else {
            System.out.println("Nie znalazlem, dodaje " + decodeString(currentRecipe.getName()));
            czestoscNazwy.addElement(decodeString(currentRecipe.getName()));
            czestoscLiczby.addElement(Integer.valueOf("1"));
        }

        Przepis.deleteAll();
        Przepis.append(new StringItem("Nazwa", decodeString(currentRecipe.getName())));
        Przepis.append(new StringItem("Kategoria", decodeString(currentRecipe.getCategory())));
        Przepis.append(new StringItem("Porcja", decodeString(String.valueOf(currentRecipe.getPortions()))));
        if(currentRecipe.getPicturePath() != null) {
            if(currentRecipe.getPicturePath().length()>3) {
                try {
                    String pathToImg = XmlDataProvider.getPathToPics() + decodeString(currentRecipe.getPicturePath());
                    InputStream is = ((FileConnection) Connector.open(pathToImg)).openInputStream();
                    Image img = Image.createImage(is);
                    Przepis.append(resizeImage(img));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        Przepis.append("\n");
        Przepis.append("Skladniki" + "\n");
        for (int i = 0; i < sizeI; i++) {

            if (measure[i] == "NO_UNIT") {
                Przepis.append(new String(ingredient[i]));
            } else {
                Przepis.append(new String(amount[i] + " " + measure[i]));
                Przepis.append(new String(ingredient[i] + "\n"));
            }
        }
        return Przepis;
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Przygotowanie ">//GEN-BEGIN:|97-getter|0|97-preInit
    /**
     * Returns an initiliazed instance of Przygotowanie component.
     * @return the initialized component instance
     */
    public Command getPrzygotowanie() {
        if (Przygotowanie == null) {//GEN-END:|97-getter|0|97-preInit
            // write pre-init user code here
            Przygotowanie = new Command("Przygotowanie", Command.OK, 0);//GEN-LINE:|97-getter|1|97-postInit
            // write post-init user code here
        }//GEN-BEGIN:|97-getter|2|
        return Przygotowanie;
    }
    //</editor-fold>//GEN-END:|97-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: PrzygotowanieLista ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of PrzygotowanieLista component.
     * @return the initialized component instance
     */
    public Form getPrzygotowanieLista() {
        if (PrzygotowanieLista == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            PrzygotowanieLista = new Form("Przygotowanie", new Item[] { getStringItem5() });//GEN-BEGIN:|99-getter|1|99-postInit
            PrzygotowanieLista.addCommand(getWróć());
            PrzygotowanieLista.addCommand(getOkCommand7());
            PrzygotowanieLista.setCommandListener(this);//GEN-END:|99-getter|1|99-postInit
            // write post-init user code here
            // form2.deleteAll();
//            for (i = 0; i < sizeA; i++) {
//                form2.append(new String(step[i]));
//            }

        }//GEN-BEGIN:|99-getter|2|
        return PrzygotowanieLista;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    public String[] listActivitiesToStep() {
        Vector listA = currentRecipe.listActivities();
        Enumeration lEnum = listA.elements();
        int sizeA = listA.size();
        String[] step = new String[sizeA];
        int i = 0;
        while (lEnum.hasMoreElements()) {
            Activity a = (Activity) lEnum.nextElement();
            step[i] = decodeString(a.getStep());
            i++;
        }
        return step;
    }
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem5 ">//GEN-BEGIN:|102-getter|0|102-preInit
    /**
     * Returns an initiliazed instance of stringItem5 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem5() {
        if (stringItem5 == null) {//GEN-END:|102-getter|0|102-preInit
            // write pre-init user code here
            stringItem5 = new StringItem("", null);//GEN-LINE:|102-getter|1|102-postInit
            // write post-init user code here

        }//GEN-BEGIN:|102-getter|2|
        return stringItem5;
    }
    //</editor-fold>//GEN-END:|102-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ticker ">//GEN-BEGIN:|96-getter|0|96-preInit
    /**
     * Returns an initiliazed instance of ticker component.
     * @return the initialized component instance
     */
    public Ticker getTicker() {
        if (ticker == null) {//GEN-END:|96-getter|0|96-preInit
            // write pre-init user code here
            ticker = new Ticker("");//GEN-LINE:|96-getter|1|96-postInit
            // write post-init user code here
        }//GEN-BEGIN:|96-getter|2|
        return ticker;
    }
    //</editor-fold>//GEN-END:|96-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Wróć1 ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of Wróć1 component.
     * @return the initialized component instance
     */
    public Command getWróć1() {
        if (Wróć1 == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            Wróć1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|105-getter|1|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|2|
        return Wróć1;
    }
    //</editor-fold>//GEN-END:|105-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Wyszukiwanie ">//GEN-BEGIN:|110-getter|0|110-preInit
    /**
     * Returns an initiliazed instance of Wyszukiwanie component.
     * @return the initialized component instance
     */
    public Command getWyszukiwanie() {
        if (Wyszukiwanie == null) {//GEN-END:|110-getter|0|110-preInit
            // write pre-init user code here
            Wyszukiwanie = new Command("Wyszukiwanie", Command.ITEM, 0);//GEN-LINE:|110-getter|1|110-postInit
            // write post-init user code here
        }//GEN-BEGIN:|110-getter|2|
        return Wyszukiwanie;
    }
    //</editor-fold>//GEN-END:|110-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Zmieńskładniki ">//GEN-BEGIN:|122-getter|0|122-preInit
    /**
     * Returns an initiliazed instance of Zmieńskładniki component.
     * @return the initialized component instance
     */
    public Command getZmieńskładniki() {
        if (Zmieńskładniki == null) {//GEN-END:|122-getter|0|122-preInit
            // write pre-init user code here
            Zmieńskładniki = new Command("Ok", Command.OK, 0);//GEN-LINE:|122-getter|1|122-postInit
            // write post-init user code here
        }//GEN-BEGIN:|122-getter|2|
        return Zmieńskładniki;
    }
    //</editor-fold>//GEN-END:|122-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">//GEN-BEGIN:|126-getter|0|126-preInit
    /**
     * Returns an initiliazed instance of okCommand2 component.
     * @return the initialized component instance
     */
    public Command getOkCommand2() {
        if (okCommand2 == null) {//GEN-END:|126-getter|0|126-preInit
            // write pre-init user code here
            okCommand2 = new Command("Zmie\u0144 sk\u0142adniki", Command.OK, 0);//GEN-LINE:|126-getter|1|126-postInit
            // write post-init user code here
        }//GEN-BEGIN:|126-getter|2|
        return okCommand2;
    }
    //</editor-fold>//GEN-END:|126-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OknoWyszukiwania ">//GEN-BEGIN:|118-getter|0|118-preInit
    /**
     * Returns an initiliazed instance of OknoWyszukiwania component.
     * @return the initialized component instance
     */
    public Form getOknoWyszukiwania() {
        if (OknoWyszukiwania == null) {//GEN-END:|118-getter|0|118-preInit
            // write pre-init user code here

            OknoWyszukiwania = new Form("Wyszukiwanie", new Item[] { getTextField(), getTextField1() });//GEN-BEGIN:|118-getter|1|118-postInit
            OknoWyszukiwania.addCommand(getOkCommand2());
            OknoWyszukiwania.addCommand(getOkCommand3());
            OknoWyszukiwania.addCommand(getBackCommand1());
            OknoWyszukiwania.setCommandListener(this);//GEN-END:|118-getter|1|118-postInit
            // write post-init user code here

        }//GEN-BEGIN:|118-getter|2|
        return OknoWyszukiwania;
    }
    //</editor-fold>//GEN-END:|118-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|120-getter|0|120-preInit
    /**
     * Returns an initiliazed instance of textField component.
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|120-getter|0|120-preInit
            // write pre-init user code here
            textField = new TextField("Nazwa", null, 32, TextField.ANY);//GEN-LINE:|120-getter|1|120-postInit
            // write post-init user code here

        }//GEN-BEGIN:|120-getter|2|
        return textField;
    }
    //</editor-fold>//GEN-END:|120-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField1 ">//GEN-BEGIN:|121-getter|0|121-preInit
    /**
     * Returns an initiliazed instance of textField1 component.
     * @return the initialized component instance
     */
    public TextField getTextField1() {
        if (textField1 == null) {//GEN-END:|121-getter|0|121-preInit
            // write pre-init user code here
            textField1 = new TextField("Sk\u0142adniki", null, 32, TextField.ANY);//GEN-LINE:|121-getter|1|121-postInit
            // write post-init user code here
            //String []element = new String[choiceGroup.size()]  ;
            int j = 0;
            if (choiceGroup != null) {
            }
        }//GEN-BEGIN:|121-getter|2|
        return textField1;
    }
    //</editor-fold>//GEN-END:|121-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: DostepneSkladniki ">//GEN-BEGIN:|124-getter|0|124-preInit
    /**
     * Returns an initiliazed instance of DostepneSkladniki component.
     * @return the initialized component instance
     */
    public Form getDostepneSkladniki() {
        if (DostepneSkladniki == null) {//GEN-END:|124-getter|0|124-preInit
            // write pre-init user code here
            DostepneSkladniki = new Form("Dost\u0119pne sk\u0142adniki", new Item[] { getChoiceGroup() });//GEN-BEGIN:|124-getter|1|124-postInit
            DostepneSkladniki.addCommand(getOkCommand4());
            DostepneSkladniki.addCommand(getBackCommand2());
            DostepneSkladniki.setCommandListener(this);//GEN-END:|124-getter|1|124-postInit
            // write post-init user code here
        }//GEN-BEGIN:|124-getter|2|
        return DostepneSkladniki;
    }
    //</editor-fold>//GEN-END:|124-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ticker1 ">//GEN-BEGIN:|117-getter|0|117-preInit
    /**
     * Returns an initiliazed instance of ticker1 component.
     * @return the initialized component instance
     */
    public Ticker getTicker1() {
        if (ticker1 == null) {//GEN-END:|117-getter|0|117-preInit
            // write pre-init user code here
            ticker1 = new Ticker("");//GEN-LINE:|117-getter|1|117-postInit
            // write post-init user code here
        }//GEN-BEGIN:|117-getter|2|
        return ticker1;
    }
    //</editor-fold>//GEN-END:|117-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand3 ">//GEN-BEGIN:|130-getter|0|130-preInit
    /**
     * Returns an initiliazed instance of okCommand3 component.
     * @return the initialized component instance
     */
    public Command getOkCommand3() {
        if (okCommand3 == null) {//GEN-END:|130-getter|0|130-preInit
            // write pre-init user code here
            okCommand3 = new Command("Ok", Command.OK, 0);//GEN-LINE:|130-getter|1|130-postInit
            // write post-init user code here
        }//GEN-BEGIN:|130-getter|2|
        return okCommand3;
    }
    //</editor-fold>//GEN-END:|130-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ListaWynikow ">//GEN-BEGIN:|133-getter|0|133-preInit
    /**
     * Returns an initiliazed instance of ListaWynikow component.
     * @return the initialized component instance
     */
    public List getListaWynikow() {
        if (ListaWynikow == null) {//GEN-END:|133-getter|0|133-preInit
            // write pre-init user code here

            ListaWynikow = new List("Lista wynik\u00F3w", Choice.IMPLICIT);//GEN-BEGIN:|133-getter|1|133-postInit
            ListaWynikow.addCommand(getWróć2());
            ListaWynikow.setCommandListener(this);//GEN-END:|133-getter|1|133-postInit
            // write post-init user code here
            for (int i = 0; i < listNameResult.length; i++) {
                ListaWynikow.append(listNameResult[i], null);
            }

        }//GEN-BEGIN:|133-getter|2|
        return ListaWynikow;
    }
    //</editor-fold>//GEN-END:|133-getter|2|

    public String[] search() {

        String ingredientSearch = encodeString(getTextField1().getString());
        String nameSearch = encodeString(getTextField().getString());
        String[] ingrSearch = split(ingredientSearch);

        String list[] = null;
        DietSearcher diet = new DietSearcher();

        if (!nameSearch.equals("") | nameSearch != null) {
            NameContainsSearcher searcher = new NameContainsSearcher(nameSearch);
            cookbook.addSearcher(searcher);
        }
        if (!ingredientSearch.equals("") | ingredientSearch != null) {
            for (int i = 0; i < ingrSearch.length; i++) {
                if (ingrSearch[i].equals("kapusta") || ingrSearch[i].equals("kiszona") || ingrSearch[i].equals("kiszona kapusta")) {
                    diet.addProduct(IngredientMenu.kiszonaKapusta);
                }
                if (ingrSearch[i].equals("cebula")) {
                    diet.addProduct(IngredientMenu.cebula);
                }
                if (ingrSearch[i].equals("boczek")) {
                    diet.addProduct(IngredientMenu.boczekWedzony);
                }
                if (ingrSearch[i].equals("jablko") || ingrSearch[i].equals("jabłko")) {
                    diet.addProduct(IngredientMenu.jablko);
                }
                if (ingrSearch[i].equals("kielbasa") || ingrSearch[i].equals("kiełbasa")) {
                    diet.addProduct(IngredientMenu.kielbasa);
                }
                if (ingrSearch[i].equals("lisc bobkowy") || ingrSearch[i].equals("liść bobkowy")) {
                    diet.addProduct(IngredientMenu.liscBobkowy);
                }
                if (ingrSearch[i].equals("pieprz")) {
                    diet.addProduct(IngredientMenu.pieprz);
                }
                if (ingrSearch[i].equals("przecier pomidorowy")) {
                    diet.addProduct(IngredientMenu.przecierPomidorowy);
                }
                if (ingrSearch[i].equals("slodka kapusta") || ingrSearch[i].equals("słodka kapusta")) {
                    diet.addProduct(IngredientMenu.slodkaKapusta);
                }
                if (ingrSearch[i].equals("sol") || ingrSearch[i].equals("sól")) {
                    diet.addProduct(IngredientMenu.sol);
                }
                if (ingrSearch[i].equals("wieprzowina")) {
                    diet.addProduct(IngredientMenu.wieprzowina);
                }
                if (ingrSearch[i].equals("ziele angielskie ")) {
                    diet.addProduct(IngredientMenu.zieleAngielskie);
                }
                if (ingrSearch[i].equals("ziele angielskie ")) {
                    diet.addProduct(IngredientMenu.zieleAngielskie);
                }
                cookbook.addSearcher(diet);

            }
            if (!nameSearch.equals("") || nameSearch != null || !ingredientSearch.equals("") || ingredientSearch != null) {
                resultList = cookbook.list(false);
                list = new String[resultList.size()];
                for (int i = 0; i < resultList.size(); i++) {
                    Recipe recipe = (Recipe) resultList.elementAt(i);
                    list[i] = decodeString(recipe.getName());
                }
            }
            textField.delete(0, textField.size());
            textField1.delete(0, textField1.size());

        }
        return list;
    }

    private String[] split(String original) {
        Vector nodes = new Vector();
        String separator = ",";
        // Parse nodes into vector
        int index = original.indexOf(separator);
        while (index >= 0) {
            nodes.addElement(original.substring(0, index));
            original = original.substring(index + separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement(original);
        // Create splitted string array
        String[] result = new String[nodes.size()];
        if (nodes.size() > 0) {
            for (int loop = 0; loop < nodes.size(); loop++) {
                result[loop] = (String) nodes.elementAt(loop);
            }
        }
        return result;
    }
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: ListaWynikowAction ">//GEN-BEGIN:|133-action|0|133-preAction
    /**
     * Performs an action assigned to the selected list element in the ListaWynikow component.
     */
    public void ListaWynikowAction() {//GEN-END:|133-action|0|133-preAction
        // enter pre-action user code here
        String __selectedString = getListaWynikow().getString(getListaWynikow().getSelectedIndex());//GEN-LINE:|133-action|1|133-postAction
        // enter post-action user code here
        for (int i = 0; i < resultList.size(); i++) {
            Recipe recipe = (Recipe) resultList.elementAt(i);
            if (__selectedString.equalsIgnoreCase(decodeString(recipe.getName()))) {
                currentRecipe = recipe;
                switchDisplayable(null, getForm());
                break;
            }
        }
    }//GEN-BEGIN:|133-action|2|
    //</editor-fold>//GEN-END:|133-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Wróć2 ">//GEN-BEGIN:|137-getter|0|137-preInit
    /**
     * Returns an initiliazed instance of Wróć2 component.
     * @return the initialized component instance
     */
    public Command getWróć2() {
        if (Wróć2 == null) {//GEN-END:|137-getter|0|137-preInit
            // write pre-init user code here
            Wróć2 = new Command("Wr\u00F3\u0107", Command.BACK, 0);//GEN-LINE:|137-getter|1|137-postInit
            // write post-init user code here
        }//GEN-BEGIN:|137-getter|2|
        return Wróć2;
    }
    //</editor-fold>//GEN-END:|137-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|140-getter|0|140-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|140-getter|0|140-preInit
            // write pre-init user code here
            backCommand1 = new Command("Wr\u00F3c", Command.BACK, 0);//GEN-LINE:|140-getter|1|140-postInit
            // write post-init user code here
        }//GEN-BEGIN:|140-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|140-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|143-getter|0|143-preInit
    /**
     * Returns an initiliazed instance of choiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {//GEN-END:|143-getter|0|143-preInit
            // write pre-init user code here
            choiceGroup = new ChoiceGroup("Lista sk\u0142adnik\u00F3w", Choice.MULTIPLE);//GEN-LINE:|143-getter|1|143-postInit
            // write post-init user code here
            choiceGroup.append("baranina", null);
            choiceGroup.append("indyk", null);
            choiceGroup.append("kurczak", null);
            choiceGroup.append("schab", null);
            choiceGroup.append("wieprzowina", null);
            choiceGroup.append("boczek", null);
            choiceGroup.append("kaczka", null);
            choiceGroup.append("parówki", null);
            choiceGroup.append("wątróbka", null);
            choiceGroup.append("mięso mielone", null);
            choiceGroup.append("cielęcina", null);
            choiceGroup.append("kiełbasa", null);
            choiceGroup.append("polędwica", null);
            choiceGroup.append("wołowina", null);

            choiceGroup.append("mintaj", null);
            choiceGroup.append("owoce morza", null);
            choiceGroup.append("tuńczyk", null);
            choiceGroup.append("karp", null);
            choiceGroup.append("pstrąg", null);
            choiceGroup.append("łosoś", null);
            choiceGroup.append("śledź", null);


            choiceGroup.append("brokuły", null);
            choiceGroup.append("chrzan", null);
            choiceGroup.append("groch", null);
            choiceGroup.append("kapusta", null);
            choiceGroup.append("papryka", null);
            choiceGroup.append("pomidory", null);
            choiceGroup.append("seler", null);
            choiceGroup.append("buraki", null);
            choiceGroup.append("czosnek", null);
            choiceGroup.append("groszek", null);
            choiceGroup.append("marchew", null);
            choiceGroup.append("pieczarki", null);
            choiceGroup.append("cebula", null);
            choiceGroup.append("fasola", null);
            choiceGroup.append("grzyby", null);
            choiceGroup.append("ogórki", null);
            choiceGroup.append("pietruszka", null);
            choiceGroup.append("sałata", null);
            choiceGroup.append("por", null);
            choiceGroup.append("ziemniaki", null);


            choiceGroup.append("brzoskwinia", null);
            choiceGroup.append("jabłko", null);
            choiceGroup.append("cytryna", null);
            choiceGroup.append("gruszka", null);
            choiceGroup.append("banany", null);


            choiceGroup.append("ser biały", null);
            choiceGroup.append("kasza", null);
            choiceGroup.append("mąka", null);
            choiceGroup.append("ryż", null);
            choiceGroup.append("ser żółty", null);
            choiceGroup.append("jajko", null);
            choiceGroup.append("masło", null);
            choiceGroup.append("margaryna", null);
            choiceGroup.append("smalec", null);
            choiceGroup.append("jogurt", null);
            choiceGroup.append("makaron", null);
            choiceGroup.append("śmietana", null);

            choiceGroup.append("bulion w kostce", null);
            choiceGroup.append("oliwa", null);
            choiceGroup.append("drożdże", null);
            choiceGroup.append("koncentrat pomidorowy", null);
            choiceGroup.append("olej", null);



        }//GEN-BEGIN:|143-getter|2|
        return choiceGroup;
    }
    //</editor-fold>//GEN-END:|143-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand4 ">//GEN-BEGIN:|144-getter|0|144-preInit
    /**
     * Returns an initiliazed instance of okCommand4 component.
     * @return the initialized component instance
     */
    public Command getOkCommand4() {
        if (okCommand4 == null) {//GEN-END:|144-getter|0|144-preInit
            // write pre-init user code here
            okCommand4 = new Command("Ok", Command.OK, 0);//GEN-LINE:|144-getter|1|144-postInit
            // write post-init user code here
        }//GEN-BEGIN:|144-getter|2|
        return okCommand4;
    }
    //</editor-fold>//GEN-END:|144-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|146-getter|0|146-preInit
    /**
     * Returns an initiliazed instance of backCommand2 component.
     * @return the initialized component instance
     */
    public Command getBackCommand2() {
        if (backCommand2 == null) {//GEN-END:|146-getter|0|146-preInit
            // write pre-init user code here
            backCommand2 = new Command("Wr\u00F3\u0107", Command.BACK, 0);//GEN-LINE:|146-getter|1|146-postInit
            // write post-init user code here
        }//GEN-BEGIN:|146-getter|2|
        return backCommand2;
    }
    //</editor-fold>//GEN-END:|146-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: przyklad ">//GEN-BEGIN:|151-getter|0|151-preInit
    /**
     * Returns an initiliazed instance of przyklad component.
     * @return the initialized component instance
     */
    public Command getPrzyklad() {
        if (przyklad == null) {//GEN-END:|151-getter|0|151-preInit
            // write pre-init user code here
            przyklad = new Command("przyklad", Command.OK, 0);//GEN-LINE:|151-getter|1|151-postInit
            // write post-init user code here
        }//GEN-BEGIN:|151-getter|2|
        return przyklad;
    }
    //</editor-fold>//GEN-END:|151-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand5 ">//GEN-BEGIN:|154-getter|0|154-preInit
    /**
     * Returns an initiliazed instance of okCommand5 component.
     * @return the initialized component instance
     */
    public Command getOkCommand5() {
        if (okCommand5 == null) {//GEN-END:|154-getter|0|154-preInit
            // write pre-init user code here
            okCommand5 = new Command("Ok", Command.OK, 0);//GEN-LINE:|154-getter|1|154-postInit
            // write post-init user code here
        }//GEN-BEGIN:|154-getter|2|
        return okCommand5;
    }
    //</editor-fold>//GEN-END:|154-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand6 ">//GEN-BEGIN:|158-getter|0|158-preInit
    /**
     * Returns an initiliazed instance of okCommand6 component.
     * @return the initialized component instance
     */
    public Command getOkCommand6() {
        if (okCommand6 == null) {//GEN-END:|158-getter|0|158-preInit
            // write pre-init user code here
            okCommand6 = new Command("Ok", Command.OK, 0);//GEN-LINE:|158-getter|1|158-postInit
            // write post-init user code here
        }//GEN-BEGIN:|158-getter|2|
        return okCommand6;
    }
    //</editor-fold>//GEN-END:|158-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand7 ">//GEN-BEGIN:|162-getter|0|162-preInit
    /**
     * Returns an initiliazed instance of okCommand7 component.
     * @return the initialized component instance
     */
    public Command getOkCommand7() {
        if (okCommand7 == null) {//GEN-END:|162-getter|0|162-preInit
            // write pre-init user code here
            okCommand7 = new Command("Dalej", Command.OK, 0);//GEN-LINE:|162-getter|1|162-postInit
            // write post-init user code here
        }//GEN-BEGIN:|162-getter|2|
        return okCommand7;
    }
    //</editor-fold>//GEN-END:|162-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OWersji1 ">//GEN-BEGIN:|166-getter|0|166-preInit
    /**
     * Returns an initiliazed instance of OWersji1 component.
     * @return the initialized component instance
     */
    public List getOWersji1() {
        if (OWersji1 == null) {//GEN-END:|166-getter|0|166-preInit
 // write pre-init user code here
            OWersji1 = new List("O wersji", Choice.IMPLICIT);//GEN-BEGIN:|166-getter|1|166-postInit
            OWersji1.append("Aplikacja w wersji 1.1", null);
            OWersji1.append("Autorzy wersji 1.0: ?", null);
            OWersji1.append("Autorzy wersji 1.1: Klaudia Grabowska, Bartosz Mi\u015Bkiewicz, Maciej Nowak, Marcin Przypek (w ramach projektu ZEO)", null);
            OWersji1.addCommand(getPowrót());
            OWersji1.setCommandListener(this);
            OWersji1.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|166-getter|1|166-postInit
 // write post-init user code here
        }//GEN-BEGIN:|166-getter|2|
        return OWersji1;
    }
    //</editor-fold>//GEN-END:|166-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: OWersji1Action ">//GEN-BEGIN:|166-action|0|166-preAction
    /**
     * Performs an action assigned to the selected list element in the OWersji1 component.
     */
    public void OWersji1Action() {//GEN-END:|166-action|0|166-preAction
 // enter pre-action user code here
        String __selectedString = getOWersji1().getString(getOWersji1().getSelectedIndex());//GEN-BEGIN:|166-action|1|177-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Aplikacja w wersji 1.1")) {//GEN-END:|166-action|1|177-preAction
 // write pre-action user code here
//GEN-LINE:|166-action|2|177-postAction
 // write post-action user code here
            } else if (__selectedString.equals("Autorzy wersji 1.0: ?")) {//GEN-LINE:|166-action|3|178-preAction
 // write pre-action user code here
//GEN-LINE:|166-action|4|178-postAction
 // write post-action user code here
            } else if (__selectedString.equals("Autorzy wersji 1.1: Klaudia Grabowska, Bartosz Mi\u015Bkiewicz, Maciej Nowak, Marcin Przypek (w ramach projektu ZEO)")) {//GEN-LINE:|166-action|5|179-preAction
 // write pre-action user code here
//GEN-LINE:|166-action|6|179-postAction
 // write post-action user code here
            }//GEN-BEGIN:|166-action|7|166-postAction
        }//GEN-END:|166-action|7|166-postAction
 // enter post-action user code here
    }//GEN-BEGIN:|166-action|8|
    //</editor-fold>//GEN-END:|166-action|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand ">//GEN-BEGIN:|175-getter|0|175-preInit
    /**
     * Returns an initiliazed instance of screenCommand component.
     * @return the initialized component instance
     */
    public Command getScreenCommand() {
        if (screenCommand == null) {//GEN-END:|175-getter|0|175-preInit
 // write pre-init user code here
            screenCommand = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|175-getter|1|175-postInit
 // write post-init user code here
        }//GEN-BEGIN:|175-getter|2|
        return screenCommand;
    }
    //</editor-fold>//GEN-END:|175-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|184-getter|0|184-preInit
    /**
     * Returns an initiliazed instance of exitCommand2 component.
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|184-getter|0|184-preInit
        // write pre-init user code here
            exitCommand2 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|184-getter|1|184-postInit
        // write post-init user code here
        }//GEN-BEGIN:|184-getter|2|
        return exitCommand2;
    }
    //</editor-fold>//GEN-END:|184-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|186-getter|0|186-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|186-getter|0|186-preInit
        // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|186-getter|1|186-postInit
        // write post-init user code here
        }//GEN-BEGIN:|186-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|186-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: OtwarciePlikuZPrzepisami ">//GEN-BEGIN:|180-getter|0|180-preInit
    /**
     * Returns an initiliazed instance of OtwarciePlikuZPrzepisami component.
     * @return the initialized component instance
     */
    public FileBrowser getOtwarciePlikuZPrzepisami() {
        if (OtwarciePlikuZPrzepisami == null) {//GEN-END:|180-getter|0|180-preInit
        // write pre-init user code here
            OtwarciePlikuZPrzepisami = new FileBrowser(getDisplay());//GEN-BEGIN:|180-getter|1|180-postInit
            OtwarciePlikuZPrzepisami.setTitle("Wybierz plik XML z przepisami");
            OtwarciePlikuZPrzepisami.setCommandListener(this);
            OtwarciePlikuZPrzepisami.addCommand(FileBrowser.SELECT_FILE_COMMAND);
            OtwarciePlikuZPrzepisami.addCommand(getExitCommand2());//GEN-END:|180-getter|1|180-postInit
        // write post-init user code here
        }//GEN-BEGIN:|180-getter|2|
        return OtwarciePlikuZPrzepisami;
    }
    //</editor-fold>//GEN-END:|180-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        try
        {
            RecordStore.deleteRecordStore("CookbookMobile");
            RecordStore rs = RecordStore.openRecordStore("CookbookMobile", true);
            
            for(int i=0; i<czestoscNazwy.size(); i++) {
                ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
                DataOutputStream out = new DataOutputStream(arrayStream);
                out.writeUTF((String)czestoscNazwy.elementAt(i) + ";" + ((Integer)czestoscLiczby.elementAt(i)).toString());
                byte[] data = arrayStream.toByteArray();
                int rsId = rs.addRecord(data, 0, data.length);
                out.close();
                arrayStream.close();
                System.out.println("Zapisalem: " + (String)czestoscNazwy.elementAt(i) + ";" + ((Integer)czestoscLiczby.elementAt(i)).toString());
            }
            
            rs.closeRecordStore();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    private Image resizeImage(Image src) {
      int srcWidth = src.getWidth();
      int srcHeight = src.getHeight();
      Canvas dummyCnv = new Canvas() {protected void paint(Graphics g) {}};
      int screenWidth = dummyCnv.getWidth();
      int screenHeight = dummyCnv.getHeight()/2;
      Image tmp = Image.createImage(screenWidth, srcHeight);
      Graphics g = tmp.getGraphics();
      int ratio = (srcWidth << 16) / screenWidth;
      int pos = ratio/2;

      //Horizontal Resize

      for (int x = 0; x < screenWidth; x++) {
          g.setClip(x, 0, 1, srcHeight);
          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }

      Image resizedImage = Image.createImage(screenWidth, screenHeight);
      g = resizedImage.getGraphics();
      ratio = (srcHeight << 16) / screenHeight;
      pos = ratio/2;

      //Vertical resize

      for (int y = 0; y < screenHeight; y++) {
          g.setClip(0, y, screenWidth, 1);
          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }
      return resizedImage;

  }//resize image

    private String decodeString(String name) {
        String tmp, p1, p2;
        char data[] = new char[36]; // ąćęłńóśźż
        String str = "ąćęłńóśźżĄĆĘŁŃÓŚŹŻ";
        data[0] = (char)196; data[1] = (char)133; //a
        data[2] = (char)196; data[3] = (char)135; //c
        data[4] = (char)196; data[5] = (char)153; //e
        data[6] = (char)197; data[7] = (char)130; //l
        data[8] = (char)197; data[9] = (char)132; //n
        data[10] = (char)195; data[11] = (char)179; //o
        data[12] = (char)197; data[13] = (char)155; //s
        data[14] = (char)197; data[15] = (char)186;//x
        data[16] = (char)197; data[17] = (char)188; //z

        data[18] = (char)196; data[19] = (char)132; //A
        data[20] = (char)196; data[21] = (char)134; //C
        data[22] = (char)196; data[23] = (char)152; //E
        data[24] = (char)197; data[25] = (char)129; //L
        data[26] = (char)197; data[27] = (char)131; //N
        data[28] = (char)195; data[29] = (char)147; //O
        data[30] = (char)197; data[31] = (char)154; //S
        data[32] = (char)197; data[33] = (char)185;//X
        data[34] = (char)197; data[35] = (char)187; //Z
        for(int i=0; i<data.length/2; i++) {
            tmp = String.valueOf(data, i*2, 2);
            while(name.indexOf(tmp)>-1) {
                p1 = name.substring(0, name.indexOf(tmp));
                p2 = name.substring(name.indexOf(tmp)+2, name.length());
                name =  p1 + str.charAt(i) + p2;
            }
        }
        return name;
    }
     private String encodeString(String name) {
        String tmp, p1, p2;
        char data[] = new char[36]; // ąćęłńóśźż
        String str = "ąćęłńóśźżĄĆĘŁŃÓŚŹŻ";
        data[0] = (char)196; data[1] = (char)133; //a
        data[2] = (char)196; data[3] = (char)135; //c
        data[4] = (char)196; data[5] = (char)153; //e
        data[6] = (char)197; data[7] = (char)130; //l
        data[8] = (char)197; data[9] = (char)132; //n
        data[10] = (char)195; data[11] = (char)179; //o
        data[12] = (char)197; data[13] = (char)155; //s
        data[14] = (char)197; data[15] = (char)186;//x
        data[16] = (char)197; data[17] = (char)188; //z

        data[18] = (char)196; data[19] = (char)132; //A
        data[20] = (char)196; data[21] = (char)134; //C
        data[22] = (char)196; data[23] = (char)152; //E
        data[24] = (char)197; data[25] = (char)129; //L
        data[26] = (char)197; data[27] = (char)131; //N
        data[28] = (char)195; data[29] = (char)147; //O
        data[30] = (char)197; data[31] = (char)154; //S
        data[32] = (char)197; data[33] = (char)185;//X
        data[34] = (char)197; data[35] = (char)187; //Z
        for(int i=0; i<data.length/2; i++) {
            tmp = String.valueOf(data, i*2, 2);
            while(name.indexOf(str.substring(i, i+1))>-1) {
                p1 = name.substring(0, name.indexOf(str.substring(i, i+1)));
                p2 = name.substring(name.indexOf(str.substring(i, i+1))+1, name.length());
                name =  p1 + tmp + p2;
            }
        }
        return name;
    }
}
